﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class PermissionReport
    {
        private ISessionFactory _sessionFactory;
        public PermissionReport()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.PermissionReport)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListPermissionReportByGroup(string sgroupid)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "select * from PERMISSION_REPORT where GROUP_ID = ? ";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.PermissionReport));
                query.SetString(0, sgroupid);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.PermissionReport GetObjectByPK(string group_id, string form_id)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from PERMISSION_REPORT where group_id = ? and FORM_NAME = ?").AddEntity(typeof(DataObjects.PermissionReport));
                query.SetString(0, group_id);
                query.SetString(1, form_id);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.PermissionReport)arrTemp[0];
                }
                return null;
            }
        }


        //code insert - update - delete 
        public void Insert(DataObjects.PermissionReport oPermissionReport)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oPermissionReport);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.PermissionReport oPermissionReport)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oPermissionReport);
                tx.Commit();		        
            }
        }

        //public void DeleteByPK(string PermissionReportId)
        //{
        //    using (ISession session = _sessionFactory.OpenSession())
        //    using (ITransaction tx = session.BeginTransaction())
        //    {
        //        DataObjects.PermissionReport oPermissionReport = (DataObjects.PermissionReport)session.Load(typeof(DataObjects.PermissionReport), PermissionReportId);
        //        session.Delete(oPermissionReport);
        //        tx.Commit();
        //    }
        //}

        public void DeleteByGroupId(string group_id)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from PERMISSION_REPORT where GROUP_ID = ?").AddEntity(typeof(DataObjects.PermissionReport));
                query.SetString(0, group_id);
                arrTemp = (ArrayList)query.List();

                foreach (DataObjects.PermissionReport oPer in arrTemp)
                {
                    session.Delete(oPer);
                }
                tx.Commit();
            }
        }
    }
}
